//
//  AppTVDetailView.m
//  classicFilms
//
//  Created by 刘继丹 on 17/3/14.
//  Copyright © 2017年 Stefan Arn. All rights reserved.
//

#import "AppTVDetailView.h"
#import <Masonry.h>
#import "ARNTVButton.h"
#import "KeyHeader.h"
#import <SDWebImageManager.h>
#import <UIImageView+WebCache.h>
#import "ARNFocusTextView.h"

@interface AppTVDetailView()<ARNFocusTextViewDelegate>
{
    NSString *content;
    NSString *nameImageStr;
}
@property(nonatomic, strong) UILabel *mNameLabel, *mWorkLabel;
@property(nonatomic, strong) UIImageView *mNameImageView;

@property(nonatomic, strong) UIView *mLine;

@property(nonatomic, strong) UIImageView *mDetailImageView;
@property(nonatomic, strong) UILabel *mDetailTitleLabel;

@property(nonatomic, strong) UIImageView *mHeaderImageView;

@property(nonatomic, strong) ARNFocusTextView *mDetailLabel;

@end

@implementation AppTVDetailView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = kDetailTopBackGroundColor_black;
     
        [self setupViews];
        [self setUpdateConstraints];
        
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];

    UIImage *image = ImageNamed(nameImageStr);
    [_mNameImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_mNameLabel.mas_right).with.offset(16);
        make.top.mas_equalTo(_mNameLabel.mas_top).with.offset((56-image.size.height)/2.f);
        make.size.mas_equalTo(CGSizeMake(image.size.width, image.size.height));
    }];
    
    _mNameImageView.image = image;
    
    if ([_dataModel.cate integerValue] == 2 || [_dataModel.cate integerValue] == 3) {
        
        [_mHeaderImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.mas_top).with.offset(0);
            make.right.mas_equalTo(self.mas_right).with.offset(0);
            make.width.mas_equalTo(1120);
            make.bottom.mas_equalTo(self.mas_bottom);
        }];
        
    }else{
      
        
        [_mHeaderImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.mas_top).with.offset(0);
            make.right.mas_equalTo(self.mas_right).with.offset(0);
            make.width.mas_equalTo(860);
            make.bottom.mas_equalTo(self.mas_bottom);
        }];
        
    }
    
}

-(void)setUpdateConstraints{
    
    [_mNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).with.offset(170);
        make.top.mas_equalTo(self.mas_top).with.offset(210);
        make.height.mas_equalTo(56);
    }];
    
    [_mWorkLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_mNameLabel.mas_left);
        make.top.mas_equalTo(_mNameLabel.mas_bottom).with.offset(30);
        make.height.mas_equalTo(28);
    }];
    
    [_mLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_mWorkLabel.mas_left);
        make.top.mas_equalTo(_mWorkLabel.mas_bottom).with.offset(50);
        make.size.mas_equalTo(CGSizeMake(870, 0.5));
    }];
    
    UIImage *detailImage = [UIImage imageNamed:@"img_information"];
    [_mDetailImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_mLine.mas_left);
        make.top.mas_equalTo(_mLine.mas_bottom).with.offset(50);
        make.size.mas_equalTo(CGSizeMake(detailImage.size.width, detailImage.size.height));
    }];
    
    [_mDetailTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_mDetailImageView.mas_right).with.offset(10);
        make.top.mas_equalTo(_mDetailImageView.mas_top).with.offset(1);
        make.height.mas_equalTo(24);
    }];


}

-(void)setupViews{
    [self addSubview:self.mNameImageView];
    [self addSubview:self.mNameLabel];
    [self addSubview:self.mWorkLabel];
    [self addSubview:self.mLine];
    [self addSubview:self.mDetailImageView];
    [self addSubview:self.mDetailTitleLabel];
    [self addSubview:self.mHeaderImageView];
}

- (BOOL)isFocusTextViewTruncated {
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:18];//调整行间距
    
    BOOL isFocusTextViewTruncated = NO;
    
    CGFloat padding = self.mDetailLabel.textContainer.lineFragmentPadding;
    CGFloat actualPageWidth = self.mDetailLabel.textContainer.size.width - padding * 2;
    
    CGSize textSize = [self.mDetailLabel.text boundingRectWithSize:CGSizeMake(actualPageWidth, CGFLOAT_MAX)
                                                                  options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                                        attributes:@{NSFontAttributeName:self.mDetailLabel.font, NSParagraphStyleAttributeName:paragraphStyle}
                                                                  context:nil].size;
    
    if (ceilf(textSize.height) > (self.mDetailLabel.textContainer.size.height)) {
        isFocusTextViewTruncated = YES;
    }
    
    return isFocusTextViewTruncated;
}

- (void)focusTextViewClicked {
    if (_detailLabelTouchBlock) {
        _detailLabelTouchBlock(content);
    }
}

-(void)setDataModel:(DetailDataModel *)dataModel{
    if (_dataModel != dataModel) {
        _dataModel = dataModel;
    }
    _mNameLabel.text = _dataModel.name;
    _mWorkLabel.text = [NSString stringWithFormat:@"%@  ", _dataModel.region_name];
    
    content = _dataModel.content;
    
    if (content.length != 0) {
        [self addSubview:self.mDetailLabel];
        _mDetailLabel.text = content;
        
        [self.mDetailLabel isTouchable:[self isFocusTextViewTruncated]];
        
    }
    
    [_mHeaderImageView sd_setImageWithURL:[NSURL URLWithString:_dataModel.big_cover]];
        
    if ([_dataModel.cate integerValue] == 1) {
        //主持人
        
        if ([_dataModel.level integerValue] == 1) {
            //首席
            nameImageStr = @"chief_compere";
            
        }else if ([_dataModel.level integerValue] == 2){
            //资深
            nameImageStr = @"senior_compere";
            
        }else if ([_dataModel.level integerValue] == 3){
            //金牌
            nameImageStr = @"goldmedal_compere";

        }
        
    }else  if ([_dataModel.cate integerValue] == 2) {
        //摄像师
        
        if ([_dataModel.level integerValue] == 1) {
            //首席
            nameImageStr = @"chief_camera";
            
        }else if ([_dataModel.level integerValue] == 2){
            //资深
            nameImageStr = @"senior_camera";
            
        }else if ([_dataModel.level integerValue] == 3){
            //金牌
            nameImageStr = @"goldmedal_camera";
            
        }
        
    }else  if ([_dataModel.cate integerValue] == 3) {
        //摄影师
        
        if ([_dataModel.level integerValue] == 1) {
            //首席
            nameImageStr = @"chief_shoot";

        }else if ([_dataModel.level integerValue] == 2){
            //资深
            nameImageStr = @"senior_shoot";
            
        }else if ([_dataModel.level integerValue] == 3){
            //金牌
            nameImageStr = @"goldmedal_shoot";
            
        }
        
    }else  if ([_dataModel.cate integerValue] == 4) {
        //化妆师
        
        if ([_dataModel.level integerValue] == 1) {
            //首席
            nameImageStr = @"chief_dresser";
            
        }else if ([_dataModel.level integerValue] == 2){
            //资深
            nameImageStr = @"senior_dresser";
            
        }else if ([_dataModel.level integerValue] == 3){
            //金牌
            nameImageStr = @"goldmedal_dresser";
            
        }
        
    }
    
}

-(UILabel *)mNameLabel{
    if (!_mNameLabel) {
        _mNameLabel = [[UILabel alloc]init];
        _mNameLabel.textColor = [UIColor whiteColor];
        _mNameLabel.font = [UIFont systemFontOfSize:56];
    }
    return _mNameLabel;
}

-(UILabel *)mWorkLabel{
    if (!_mWorkLabel) {
        _mWorkLabel = [[UILabel alloc]init];
        _mWorkLabel.textColor = [UIColor whiteColor];
        _mWorkLabel.font = [UIFont systemFontOfSize:28];
    }
    return _mWorkLabel;
}

-(UIImageView *)mNameImageView{
    if (!_mNameImageView) {
        _mNameImageView = [[UIImageView alloc]init];
        
    }
    return _mNameImageView;
}

-(UIView *)mLine{
    if (!_mLine) {
        _mLine = [[UIView alloc]init];
        _mLine.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.12];
    }
    return _mLine;
}

-(UIImageView *)mDetailImageView{
    if (!_mDetailImageView) {
        _mDetailImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"img_information"]];
    }
    return _mDetailImageView;
}

-(UILabel *)mDetailTitleLabel{
    if (!_mDetailTitleLabel) {
        _mDetailTitleLabel = [[UILabel alloc]init];
        _mDetailTitleLabel.textColor = [UIColor whiteColor];
        _mDetailTitleLabel.font = [UIFont systemFontOfSize:24];
        _mDetailTitleLabel.text = @"详细信息";
    }
    return _mDetailTitleLabel;
}

-(ARNFocusTextView *)mDetailLabel{
    if (!_mDetailLabel) {
        _mDetailLabel = [[ARNFocusTextView alloc] initWithFrame:CGRectMake(150, 480, 870, 195)];
        _mDetailLabel.backgroundColor = [UIColor clearColor];
        _mDetailLabel.font = [UIFont systemFontOfSize:22];
        _mDetailLabel.textContainer.maximumNumberOfLines = 5;
        _mDetailLabel.textColor = [UIColor whiteColor];
        _mDetailLabel.layer.shadowColor = [[UIColor blackColor] CGColor];
        _mDetailLabel.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
        _mDetailLabel.layer.shadowOpacity = 2.0f;
        _mDetailLabel.layer.shadowRadius = 2.0f;
        _mDetailLabel.focusTextViewDelegate = self;
    }
    return _mDetailLabel;
}

-(UIImageView *)mHeaderImageView{
    if (!_mHeaderImageView) {
        _mHeaderImageView = [[UIImageView alloc]init];
    }
    return _mHeaderImageView;
}

@end
